﻿using Newtonsoft.Json.Serialization;
using System;
using System.Collections.Generic; 

namespace Yunzuan.Foundation.Web_api
{
    //格式化中心
    public class DynamicContractResolver : DefaultContractResolver
    {
        private readonly string[] _startingWithValues;

        public DynamicContractResolver(string[] startingWithChar)
        {
            _startingWithValues = startingWithChar;
        }

        protected override IList<JsonProperty> CreateProperties(Type type, Newtonsoft.Json.MemberSerialization memberSerialization)
        {
            IList<JsonProperty> properties = base.CreateProperties(type, memberSerialization);  //all key

            for (int i = 0; i < _startingWithValues.Length; i++)
            {
                for (int j = 0; j < properties.Count; j++)
                {
                    if (properties[j].PropertyName == _startingWithValues[i])
                    {
                        properties.Remove(properties[j]);
                        break;
                    }
                }
            }

            //   properties.Where(p => p.PropertyName.StartsWith(_startingWithChar.ToString())).ToList();
            return properties;
        }
    }
}